<?php

	/**
	 * File : computers.php
	 * Description : Condor's nodes managment page
	 * Date : 07.08.2012 14:46:05
	 * 
	 * This file is part of Phenix.  Phenix is free software: you can
	 * redistribute it and/or modify it under the terms of the GNU General Public
	 * License as published by the Free Software Foundation, version 2.
	 *
	 * This program is distributed in the hope that it will be useful, but WITHOUT
	 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
	 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
	 * details.
	 *
	 * You should have received a copy of the GNU General Public License along with
	 * this program; if not, write to the Free Software Foundation, Inc., 51
	 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
	 *
	 * Nils Schaetti <n.schaetti@gmail.com>
	**/

	// Model
	define('MODEL','../models/main/home.php.html');

	// Includes
	require_once('../core/libphenix.php');
	require_once('../KTK/ktk.php');

	// Buffering
	ob_start();

	// Démarre la session
	session_start();
	
	// Variables GET
	if(!isset($_GET['dir']))
		$dir = '.';
	else
		$dir = $_GET['dir'];
	
	// Paramètres du modèle
	$params = array();
	
	// Nouvel entête
	$header = new ktkHtmlHeader('../KTK/css/style.css','../KTK/js','Phenix Web Grid');
	$header->addCSSFile('../css/style.css');
	$header->addJSFile('../js/functions.js');
	//$header->setOnLoad("phenixListDirectory('phenixListView','$dir');");
	$params['@@_HEADER_@@'] = $header->getCode();
	
	// Connexion ssh
	if(!($ssh_handler = lp_ssh_connect('localhost',$_SESSION['username'],$_SESSION['password'])))
	{
		echo "Erreur de connexion<br>";
		exit;
	}
	
	// Envoi le code
	$params['@@_HEADERBAR_@@'] = phenix_HeaderBar();
	
	// Envoi le code
	$params['@@_MENUBAR_@@'] = phenix_MainToolBar('../');
	
	// Crée la mini toolbar
	$minitoolbar = new ktkMiniToolbar('phenixMiniToolbar','Home');
	
	// Ajoute les boutons
	$minitoolbar->addButton('mtNewDir', new ktkMiniToolbarButton('mtNewDir','../images/folder-new.png',"javascript: phenixNewDirectoryDialog(event);",'left',false,false));
	$minitoolbar->addButton('mtUpDir', new ktkMiniToolbarButton('mtUpDir','../images/up_dir.png',"javascript: phenixUpDirectory('phenixListView');",'left',false,false));
	$minitoolbar->addButton('mtDelete', new ktkMiniToolbarButton('mtDelete','../images/delete.png',"javascript: phenixDeleteButton('phenixListView');",'left',false,false));
	$minitoolbar->addButton('mtShowHidden', new ktkMiniToolbarButton('mtShowHidden','../images/hidden-files.png',"javascript: phenixShowHiddenFiles();",'right',false,false));
	$minitoolbar->addButton('mtOrderTime', new ktkMiniToolbarButton('mtOrderTime','../images/order_clock.png',"javascript: phenixOrderByTime();",'right',true,false));
	$minitoolbar->addButton('mtOrderOwner', new ktkMiniToolbarButton('mtOrderOwner','../images/order_owner.png',"javascript: phenixOrderByOwner();",'right',true,true));
	$minitoolbar->addButton('mtOrderFilename', new ktkMiniToolbarButton('mtOrderFilename','../images/order_filename.png',"javascript: phenixOrderByFilename();",'right',false,true));
	
	// Cache le bouton "Supprimer"
	$minitoolbar->hideButton('mtDelete');
	
	// Envoi le code
	$params['@@_MINITOOLBAR_@@'] = $minitoolbar->getCode();
	
	// Code vide
	$code = '';
				
	// Execute une commande
	if(($files = lp_ls($ssh_handler,$dir,false,'')) == true)
	{
		// Pour chaque fichier;
		if(count($files) > 0 && $files !== '')
		{
			foreach($files as $file)
			{
				if($file->is_dir())
				{
					$code .= '<li>
							<a href="home.php?dir='.$dir.'/'.$file->getFilename().'">
							<img src="../images/file_types/folder.png">
							'.$file->getFilename().'
							</a>
							</li>';
				}
			}
		}
	}
			
	// Paramètres répertoires		
	$params['@@_DIRS_@@'] = $code;
	
	// Nouveau ListView
	$listView = new ktkListView('phenixListView');
	
	// Ajoute les colonnes
	$listView->addColumn('', 'TypeColumn', COLUMN_TYPE_IMAGE);
	$listView->addColumn('NAME', 'FilenameColumn', COLUMN_TYPE_TEXT);
	$listView->addColumn('OPSYS', 'AccessRightColumn', COLUMN_TYPE_TEXT);
	$listView->addColumn('ARCH', 'OwnerColumn',COLUMN_TYPE_TEXT);
	$listView->addColumn('STATE', 'SizeColumn', COLUMN_TYPE_TEXT);
	$listView->addColumn('ACTIVITY', 'LastModificationColumn', COLUMN_TYPE_TEXT);
	$listView->addColumn('LOADAVG', 'RenameColumn', COLUMN_TYPE_TEXT);
	$listView->addColumn('MEM', 'DownloadColumn', COLUMN_TYPE_TEXT);
	$listView->addColumn('ACTVTYTIME', 'DownloadColumn', COLUMN_TYPE_TEXT);
	
	// Remplacement
	$params['@@_FILES_@@'] = $listView->getCode();

	// Pied de page HTML
	$params['@@_FOOTER_@@'] = ktkHTMLFooter();
	
	// Envoi le modèle
	echo sbGetModel(MODEL, $params);

	// Ferme la connexion ssh
	lp_ssh_close($ssh_handler);	

	// Fin buffering
	ob_end_flush();

?>

